package gui;

import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JScrollBar;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

import java.awt.Color;
import javax.swing.AbstractListModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import messagesPacks.LibrarianSearchUserMessage;

import controllers.LibrarianUIP;
import controllers.OBLSettings;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This class representing a Librarian Add or Edit User Gui
 * Date: 29/12/2012
 * @author Tali
 *
 */


public class LibrarianAddEditUserUI extends JPanel 
{
	
	    private static final long serialVersionUID = 1L; 
	    private LibrarianUIP luip=null;
		JTextField txtAddUserIDField;
		JTextField txtFirstNameField;
		JTextField txtLastNameField;
		private JLabel lblUserID;
		private JLabel lblFirstNameUser;
		private JSeparator separator;
		public JLabel lblTitleLabel;
	    public JButton btnUserOperation;
	    private JButton btnBack;
	    private JLabel lblAddressUser;
	    private JLabel lblEmailUser;
	    private JLabel lblPhoneNumUser;
	    JTextField txtEmailField;
	    JTextField txtPhoneNumField;
	    LibrarianUI librarianPanel=null;
	    JTextField txtAddressField;
	    JButton btnExitButton;
	    JButton btnLogoutButton;
	    JLabel lblStatus;
	    JTextField txtStatusField;
	    private JLabel backgroundlabel;
	    
//===============================================================================================
//		 Constructor
//		 
/**
* Constructor the LibrarianAddEditUserUI with default values
* @param librarianUI
*/	    	 
	    
		public LibrarianAddEditUserUI(LibrarianUI librarianUI) 
		{
			super();
			this.librarianPanel=librarianUI;
			this.luip=new LibrarianUIP();
			initialize();	
		}

		private void initialize()
		{
		   this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
			setLayout(null);
			
			
			txtStatusField = new JTextField();
			txtStatusField.setColumns(10);
			txtStatusField.setBounds(159, 408, 265, 34);
			add(txtStatusField);
			
			txtAddUserIDField = new JTextField();
			txtAddUserIDField.setBounds(159, 93, 265, 34);
			add(txtAddUserIDField);
			txtAddUserIDField.setColumns(10);
			
			txtFirstNameField = new JTextField();
			txtFirstNameField.setBounds(159, 147, 265, 34);
			add(txtFirstNameField);
			txtFirstNameField.setColumns(10);
			
			txtLastNameField = new JTextField();
			txtLastNameField.setBounds(159, 199, 265, 34);
			add(txtLastNameField);
			txtLastNameField.setColumns(10);
			
			lblUserID = new JLabel("User ID :");
			lblUserID.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblUserID.setBounds(23, 93, 103, 31);
			add(lblUserID);
			
			lblFirstNameUser = new JLabel("First Name :");
			lblFirstNameUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblFirstNameUser.setBounds(23, 153, 174, 24);
			add(lblFirstNameUser);
			
			JLabel lblLastNameUser = new JLabel("Last Name :");
			lblLastNameUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblLastNameUser.setBounds(23, 208, 174, 24);
			add(lblLastNameUser);
			
			separator = new JSeparator();
			separator.setBounds(0, 57, 881, 24);
			add(separator);
			
			System.out.println("op_type = " + librarianPanel.op_user);
			
			lblTitleLabel = new JLabel("OBL   Add New User");
			
			lblTitleLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
			lblTitleLabel.setBounds(267, -1, 247, 60);
			add(lblTitleLabel);
			
			btnLogoutButton = new JButton("Logout");
			btnLogoutButton.setFont(new Font("Tahoma", Font.BOLD, 15));
			btnLogoutButton.setBounds(603, 11, 83, 41);
			add(btnLogoutButton);
			
			btnExitButton = new JButton("Exit");
		
			btnExitButton.setFont(new Font("Tahoma", Font.BOLD, 15));
			btnExitButton.setBounds(696, 11, 83, 41);
			add(btnExitButton);
			
			if (librarianPanel.op_user.equals("AddUser")) 
				btnUserOperation = new JButton("Add User");
			else
				btnUserOperation = new JButton("Edit User");
			
			btnUserOperation.setFont(new Font("Tahoma", Font.PLAIN, 15));
			btnUserOperation.setBounds(321, 505, 142, 41);
			add(btnUserOperation);
			
			btnBack = new JButton("Back");
			btnBack.setFont(new Font("Tahoma", Font.BOLD, 15));
			btnBack.setBounds(10, 11, 83, 41);
			add(btnBack);
			
			lblAddressUser = new JLabel("Address :");
			lblAddressUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblAddressUser.setBounds(23, 263, 174, 24);
			add(lblAddressUser);
			
			txtAddressField = new JTextField();
			txtAddressField.setColumns(10);
			txtAddressField.setBounds(159, 251, 265, 34);
			add(txtAddressField);
			
			lblEmailUser = new JLabel("E-mail :");
			lblEmailUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblEmailUser.setBounds(23, 318, 174, 24);
			add(lblEmailUser);
			
			lblPhoneNumUser = new JLabel("Phone Number :");
			lblPhoneNumUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblPhoneNumUser.setBounds(10, 365, 116, 24);
			add(lblPhoneNumUser);
			
			txtEmailField = new JTextField();
			txtEmailField.setColumns(10);
			txtEmailField.setBounds(159, 303, 265, 34);
			add(txtEmailField);
			
			txtPhoneNumField = new JTextField();
			txtPhoneNumField.setColumns(10);
			txtPhoneNumField.setBounds(159, 355, 265, 34);
			add(txtPhoneNumField);
			
			lblStatus = new JLabel("Status :");
			lblStatus.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblStatus.setBounds(23, 411, 116, 24);
			add(lblStatus);
			
			backgroundlabel = new JLabel("");
			backgroundlabel.setIcon(OBLSettings.IMG_LIBRARIAN_USER_BOOK_COPY);
			backgroundlabel.setBounds(0, 10, 800, 600);
			add(backgroundlabel);

			
// ===========================================================================================
//			 Action Listeners
//          
			//back button (back to librarian main panel)
			btnBack.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					WelcomeUI.wui.switchPanel("OBL  - Librarian", librarianPanel, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
					txtAddUserIDField.setText(null);
					txtFirstNameField.setText(null);
					txtLastNameField.setText(null);
					txtEmailField.setText(null);
					txtPhoneNumField.setText(null);
					txtAddressField.setText(null);
					txtStatusField.setText(null);
					librarianPanel.btnEdit.setEnabled(false);
					librarianPanel.btnRemove.setEnabled(false);
					librarianPanel.searchResultTable.model.setRowCount(0);
					
				}
			});
			
			btnUserOperation.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					if (librarianPanel.op_user.equals("AddUser"))
					{
						
						int createUserRes=luip.createUser(getUserID(),"12345",getUserFName(),getUserLName(),getUserAddress(),getUserEmail(),getUserPhone());
						if (!getUserID().isEmpty() &&  !getUserFName().isEmpty() && !getUserLName().isEmpty() && !getUserEmail().isEmpty() && !getUserPhone().isEmpty() && !getUserAddress().isEmpty()) 
						{
							switch (createUserRes)
							{
								case 1: // User created successfully
								{
									txtAddUserIDField.setText(null);
									txtFirstNameField.setText(null);
									txtLastNameField.setText(null);
									txtEmailField.setText(null);
									txtPhoneNumField.setText(null);
									txtAddressField.setText(null);
									
						
									//JOptionPane.showMessageDialog( WelcomeUI.wui, "User created successfully!","Create User",JOptionPane.INFORMATION_MESSAGE);
									break;
								} 
								case -1: // User already exists
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, "User already exists!","Warning",JOptionPane.WARNING_MESSAGE);
									break;
								}
								default: // Error
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Warning",JOptionPane.ERROR_MESSAGE);				
									break;
								}
							} // End switch			
						}
						else 
							JOptionPane.showMessageDialog( WelcomeUI.wui, "Not all fields are filled!","Warning",JOptionPane.WARNING_MESSAGE);
					
					} // AddUser
					else
					{//Edit user
						String userID = getUserID();
						LibrarianSearchUserMessage librarianSearchUserMsg = new LibrarianSearchUserMessage(userID);
						if (userID.isEmpty())
						{
							JOptionPane.showMessageDialog( WelcomeUI.wui, "The field UserID is empty .\n Please enter user ID " 
									,"Warning",JOptionPane.WARNING_MESSAGE);
							
						}
						else
						{	
							librarianSearchUserMsg = luip.librarianSearchUser(userID);

							//user Doesn't exist
							if (librarianSearchUserMsg.getQueryUserSearchResult() == -2)
							{
							JOptionPane.showMessageDialog( WelcomeUI.wui, "There user doesn't exist" ,
								"Warning",JOptionPane.WARNING_MESSAGE);

							}
							//error in db
							else if (librarianSearchUserMsg.getQueryUserSearchResult()==-3)
							{
								JOptionPane.showMessageDialog( WelcomeUI.wui, "There was an error in search user" ,
									"Warning",JOptionPane.WARNING_MESSAGE);
							}
							else
							{
			
								int editUserRes=luip.editUser(getUserID(),getUserFName(),getUserLName(),getUserAddress(),getUserEmail(),getUserPhone());
								switch (editUserRes)
								{
									case 1: // user update successfully
									{
										JOptionPane.showMessageDialog( WelcomeUI.wui, " Edit succeded ","Done",JOptionPane.INFORMATION_MESSAGE);
	                                    break;								
									}
									case 0: // Error
									{
										JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Warning",JOptionPane.ERROR_MESSAGE);				
										break;
									}
								}//switch


							}//else
						}	
						
					} // EditUser 
				} 

			});
	
		}

//====================================================================================	
//        Getters & Setters
//

		public String getUserID()
		{
			return this.txtAddUserIDField.getText();
		}

		
		public String getUserFName()
		{
			return this.txtFirstNameField.getText();
		}
		
		public String getUserLName()
		{
			return this.txtLastNameField.getText();
		}
		
		public String getUserEmail()
		{
			return this.txtEmailField.getText();
		}
		
		public String getUserPhone()
		{
			return this.txtPhoneNumField.getText();
		}
		
		public String getUserAddress()
		{
			return this.txtAddressField.getText();
		}
		
		
		public void setUserID(JTextField txtAddUserIDField)
		{
				this.txtAddUserIDField=txtAddUserIDField;
		}
		
		
		public void setUserFName(JTextField txtFirstNameField)
		{
				this.txtFirstNameField=txtFirstNameField;
		}
		
		public void setUserLName(JTextField txtLastNameField )
		{
				this.txtLastNameField=txtLastNameField;
		}
		
		public void setUserEmail(JTextField txtEmailField)
		{
				this.txtEmailField=txtEmailField;
		}
		
		public void setUserPhone(JTextField txtPhoneNumField)
		{
				this.txtPhoneNumField=txtPhoneNumField;
		}
		
		public void setUserAddress(JTextField txtAddressField )
		{
			 	this.txtAddressField=txtAddressField;
		}
		
		public void paintComponent(Graphics g) 
		{
			g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
			
		}

}
		
		

